<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<title>用户登录</title>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">

</head>
<body>

<div class="container mt-3" >

<p style="font-size: 16px;font-weight: bold;" class="mb-3">亲~欢迎您使用此工具</p>	
<nav class="nav nav-tabs" id="myTab" role="tablist">
  <a class="nav-item nav-link active" id="nav-home-tab" data-toggle="tab" href="#nav-home" role="tab" aria-controls="nav-home" aria-selected="true">登录</a>
  <a class="nav-item nav-link" id="nav-profile-tab" data-toggle="tab" href="#nav-profile" role="tab" aria-controls="nav-profile" aria-selected="false">注册</a>
 
</nav>
<div class="tab-content mt-3 mb-3" id="nav-tabContent">
  <div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">


		  <div class="form-group">
		    <label for="login-user">用户</label>
		    <input type="text" class="form-control" id="login-user"  placeholder="用户名">
		    <!-- <small class="form-text text-muted">We'll never share your email with anyone else.</small> -->
		  </div>
		  <div class="form-group">
		    <label for="login-pwd">密码</label>
		    <input type="password" class="form-control" id="login-pwd" placeholder="密码">
			<!-- <small class="form-text text-muted">We'll never share your email with anyone else.</small> -->
		  </div>

		  <button id="btn-login" class="btn btn-primary">登录</button>

		  <button  class="btn btn-secondary ml-3 return-index">返回主页</button>

  </div>
  <div class="tab-pane fade" id="nav-profile" role="tabpanel" aria-labelledby="nav-profile-tab">

		  <div class="form-group">
		    <label for="reg-user">用户</label>
		    <input type="text" class="form-control" id="reg-user"  placeholder="请输入用户名">
		    <small class="form-text text-muted">不能超过18个字符. <span style="color: red">必填</span></small>
		  </div>
		  <div class="form-group">
		    <label for="reg-pwd">密码</label>
		    <input type="password" class="form-control" id="reg-pwd" placeholder="请输入密码">
		    <small class="form-text text-muted">不能超过18个字符. <span style="color: red">必填</span></small>
		  </div>
		  <div class="form-group">
		    <label for="reg-pwd">确认密码</label>
		    <input type="password" class="form-control" id="reg-repwd" placeholder="请再次输入密码">
		    <small class="form-text text-muted">不能超过18个字符 。两次密码要一致. <span style="color: red">必填</span></small>
		  </div>
		  <div class="form-group">
		    <label for="reg-pwd">邀请码</label>
		    <input type="text" class="form-control" id="reg-code"  placeholder="请输入别人的邀请码">
		    <small class="form-text text-muted">没有可不填 。填错则邀请码不生效</small>
		  </div>
		 
		  <button  id="btn-register" class="btn btn-primary">提交</button>

		  <button  class="btn btn-secondary ml-3  return-index">返回主页</button>

		

  </div>

</div>
	


	

</div>

<!-- jQuery first, then Tether, then Bootstrap JS.  B-->

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
 
<!-- jQuery first, then Tether, then Bootstrap JS. E -->

<script>
//获取url 参数
 	function getUrlParameter (name){
    	return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
	}


	var isReg = getUrlParameter('regPar');

	var codeReg = getUrlParameter('codeReg');

	if(isReg && isReg == 'reg'){
		reg();
	}

	if(codeReg){
		$("#reg-code").val(codeReg).attr("disabled","disabled");
	}


	function reg(){
		
		$('.tab-pane').removeClass('active').removeClass('show');
		$('#nav-profile').addClass('active').addClass('show');

		$('.nav-item').removeClass('active').attr('aria-selected','false');
		$('#nav-profile-tab').addClass('active').attr('aria-selected','true');
	}




(function(){
	$(".return-index").off().on("click", function(){
		window.location.href='index.html';
	});
	//去左右空格;
	function trim(s){
	    return s.replace(/(^\s*)|(\s*$)/g, "");
	}
	
	//登录
	
	$("#btn-login").off().on("click", login);
	function login(){
		var loginUser = trim($("#login-user").val());
		var loginPwd =  trim($("#login-pwd").val());

		if(!loginUser || loginUser.length == 0){
				alert("登录用户不能为空");
				return;
		}
		if(!loginPwd || loginPwd.length == 0){
				alert("登录密码不能为空");
				return;
		}

		
		var loginUrl = "/mytk/tklUser/login";

		$("#btn-login").off();
		$.ajax({
		   type: "POST",
		   url: loginUrl,
		   data: "name="+loginUser+"&pwd="+loginPwd,
		   dataType:"json",
		   success: function(msg){
		   		$("#btn-login").off().on("click", login);
			   	console.info(msg)
		
			   	if(msg.code == 200){
			   		window.location.href='index.html';
			   		
			   	}else{
			   		alert(msg.msg)
			   	}
			   	
		   },
		   error: function(XMLHttpRequest, textStatus, errorThrown) {
	
				$("#btn-login").off().on("click", login);
				alert("请求失败！请联系管理员");
		   }


		})

	}



	//注册
	$("#btn-register").off().on("click", register);
	function register(){
		var regUser = trim($("#reg-user").val());
		var regPwd = trim($("#reg-pwd").val());
		var regRepwd = trim($("#reg-repwd").val());
		var regCode = trim($("#reg-code").val());

		if(!regUser || regUser.length == 0){
				alert("注册用户不能为空");
				return;
		}
		if(!regPwd || regPwd.length == 0){
				alert("注册密码不能为空");
				return;
		}
		if(regPwd != regRepwd){
				alert("两次输入密码不一样");
				return;
		}

		var regUrl = "/mytk/tklUser/register";

		$("#btn-register").off();
		$.ajax({
		   type: "POST",
		   url: regUrl,
		   data: "name="+regUser+"&pwd="+regPwd+"&byInvitationCode="+regCode,
		   dataType:"json",
		   success: function(msg){
		   		$("#btn-register").off().on("click", register);
			   	console.info(msg)

			   	if(msg.code == 200){
			   		alert('注册成功');
					window.location.href='index.html';
			   	}else{
			   		alert(msg.msg)
			   	}

		   },
		   error: function(XMLHttpRequest, textStatus, errorThrown) {

				$("#btn-register").off().on("click", register);
				alert("请求失败！请联系管理员");
		   }


		})

	}


}())


</script>

</body>
</html>